﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JXT.PrimaryKey.Batman.Extensions
{
    public static class NumberExtensions
    {
        /// <summary>
        /// 将数字进行四舍五入格式化，然后返回数字
        /// </summary>
        /// <param name="value">数字值</param>
        /// <param name="len">小数长度</param>
        /// <returns>格式化后的数字</returns>
        public static double ToFixed(this double value, int len)
        {
            string format = "{0:#0." + new String('0', len) + "}";
            double result;
            if (!double.TryParse(String.Format(format, value), out result))
            {
                result = 0d;
            }
            return result;
        }

        /// <summary>
        /// 将数字进行四舍五入格式化，然后返回数字
        /// </summary>
        /// <param name="value">数字值</param>
        /// <param name="len">小数长度</param>
        /// <returns>格式化后的数字</returns>
        public static decimal ToFixed(this decimal value, int len)
        {
            string format = "{0:#0." + new String('0', len) + "}";
            decimal result;
            if (!decimal.TryParse(String.Format(format, value), out result))
            {
                result = 0;
            }
            return result;
        }
    }
}
